﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        String template_dir = "";
        String sample_dir = "";
        String thresh_value = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string resultStr = "";
            thresh_value = textBox3.Text;
            string strInput1 = "conda activate myopencv";
            string strInput2 = "python F:/PycharmProjects/MatchTemplate/demo_defect_detection.py " + template_dir + " " + sample_dir + " " + thresh_value;
            
            RunCMDCommand(out resultStr, strInput1, strInput2);

            Console.WriteLine(resultStr);
        }
        public void RunCMDCommand(out string outPut, params string[] command)
        {
            using (Process pc = new Process())
            {
                pc.StartInfo.FileName = "cmd.exe";
                //pc.StartInfo.CreateNoWindow = false;//隐藏窗口运行
                pc.StartInfo.RedirectStandardError = true;//重定向错误流
                pc.StartInfo.RedirectStandardInput = true;//重定向输入流
                pc.StartInfo.RedirectStandardOutput = true;//重定向输出流
                pc.StartInfo.UseShellExecute = false;
                pc.Start();
                int lenght = command.Length;
                foreach (string com in command)
                {
                    pc.StandardInput.WriteLine(com);//输入CMD命令
                }
                pc.StandardInput.WriteLine("exit");//结束执行，很重要的
                pc.StandardInput.AutoFlush = true;

                outPut = pc.StandardOutput.ReadToEnd();//读取结果        

                pc.WaitForExit();
                pc.Close();
            }
        }
        public string GetOpenFileDialogReturnFileFullName(bool multiSelect = false)

        {
            System.Windows.Forms.OpenFileDialog opdialog = new System.Windows.Forms.OpenFileDialog();
            opdialog.Multiselect = multiSelect;
            opdialog.Filter = "bmp|*.bmp";
            string files = null;
            if (opdialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                files = System.IO.Path.GetFullPath(opdialog.FileName);                             //绝对路径
            }

            return files;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            template_dir = GetOpenFileDialogReturnFileFullName();
            textBox1.Text = template_dir;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            sample_dir = GetOpenFileDialogReturnFileFullName();
            textBox2.Text = sample_dir;
        }
    }
}
